// 1. 饿汉式（线程安全）
class EagerSingleton {
    private static final EagerSingleton INSTANCE = new EagerSingleton();

    private EagerSingleton() {
        // 防止反射创建实例
        if (INSTANCE != null) {
            throw new IllegalStateException("Singleton already initialized");
        }
    }

    public static EagerSingleton getInstance() {
        return INSTANCE;
    }
}

// 2. 懒汉式（非线程安全）
class LazySingleton {
    private static LazySingleton instance;

    private LazySingleton() {}

    public static LazySingleton getInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

// 3. 同步方法版（线程安全低效）
class SynchronizedSingleton {
    private static SynchronizedSingleton instance;

    private SynchronizedSingleton() {}

    public static synchronized SynchronizedSingleton getInstance() {
        if (instance == null) {
            instance = new SynchronizedSingleton();
        }
        return instance;
    }
}

// 4. 双重检查锁（DCL）
class DCLSingleton {
    // volatile禁止指令重排序（JDK5+生效）
    private static volatile DCLSingleton instance;

    private DCLSingleton() {
        // 防御反射攻击
        if (instance != null) {
            throw new IllegalStateException("Already initialized");
        }
    }

    public static DCLSingleton getInstance() {
        DCLSingleton result = instance;
        if (result == null) {  // 第一次检查
            synchronized (DCLSingleton.class) {
                result = instance;
                if (result == null) {  // 第二次检查
                    instance = result = new DCLSingleton();
                }
            }
        }
        return result;
    }
}

// 5. 静态内部类（最优懒加载）
class InnerClassSingleton {
    private InnerClassSingleton() {
        // 防止反射破坏
        if (Holder.INSTANCE != null) {
            throw new IllegalStateException("Already initialized");
        }
    }

    public static InnerClassSingleton getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        static final InnerClassSingleton INSTANCE = new InnerClassSingleton();
    }
}

// 6. 枚举实现（最安全）
enum EnumSingleton {
    INSTANCE;

    // 添加业务方法
    public void businessMethod() {
        // 业务逻辑
    }
}
